package com.actions;

import javax.naming.InitialContext;
import javax.servlet.http.HttpServletRequest;
import com.entity.Usuario;
import com.ejb.UsuarioFachadaRemote;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author victor
 */
public class LoginActionForm extends org.apache.struts.action.ActionForm {

    private UsuarioFachadaRemote UsuarioFachadaBean;

    private String name;

    private int number;

    private String Nickl;
    private String Passl;

    public String getNickl() {
        return Nickl;
    }

    public void setNickl(String Nickl) {
        this.Nickl = formatear(Nickl,15);
    }

    public String getPassl() {



        return Passl;
    }

    public void setPassl(String Passl) {
        this.Passl = formatear(Passl,15);
    }

    /**
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     * @param string
     */
    public void setName(String string) {
        name = string;
    }

    /**
     * @return
     */
    public int getNumber() {
        return number;
    }

    /**
     * @param i
     */
    public void setNumber(int i) {
        number = i;
    }

    /**
     *
     */
    public LoginActionForm() {
        super();

    }

    @Override
     public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();

        if (getNickl() == null || getNickl().length() < 1)
        {
            errors.add("nickl", new ActionMessage("error.nick.requerido"));
        }
        if (getPassl() == null || getPassl().length() < 1)
        {
            errors.add("passl", new ActionMessage("error.pass.requerido"));
        }
         if (errors.isEmpty()) // ValidaciÃ³n ahora en la base de datos
        {
            // Llamada al Enterprise Bean
            try
            {
                UsuarioFachadaBean = (UsuarioFachadaRemote)new InitialContext().lookup("java:comp/env/UsuarioFachadaBean");
            }catch (Exception e) {}
            if (UsuarioFachadaBean!=null)
            {
                Usuario miUsuario = null;
                miUsuario = UsuarioFachadaBean.ExisteUsuario(getNickl(),getPassl());
                if (miUsuario==null)
                {
                    errors.add("passl", new ActionMessage("error.nick.noexiste"));
                }else
                {
                    request.getSession().setAttribute("Id", miUsuario.getId());
                    request.setAttribute("Id", miUsuario.getId());
                    request.getSession().setAttribute("Nick", miUsuario.getNick());
                    request.getSession().setAttribute("Nombre", miUsuario.getNombre());
                    request.getSession().setAttribute("Apellido", miUsuario.getApellido());
                    request.getSession().setAttribute("Email", miUsuario.getEmail());
                    if (miUsuario.isEsAdministrador())
                        request.getSession().setAttribute("Tipo", "Admin");
                    else request.getSession().setAttribute("Tipo", "Comun");
                }
            }
        }
        return errors;
    }
public String formatear(String texto, int largo)
{
    texto=texto.replaceAll("Ã¡","á");
    texto=texto.replaceAll("Ã","Á");

    texto=texto.replaceAll("Ã©","é");
    texto=texto.replaceAll("Ã","É");

    texto=texto.replaceAll("Ã","Í");
    texto=texto.replaceAll("Ãº","ú");

    texto=texto.replaceAll("Ã","Ú");
    texto=texto.replaceAll("Ã±","ñ");

    texto=texto.replaceAll("Ã","Ñ");
    texto=texto.replaceAll("Ã³","ó");

    texto=texto.replaceAll("Ã","Ó");
    texto=texto.replaceAll("Ã","í");

    texto=texto.replaceAll("­Â­","");
    texto=texto.replaceAll("­","");

    if (texto.length()>largo) texto = texto.substring(0, largo);
    return texto;
}

}
